package com.example.shiro_ini;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.env.Environment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class TestIniRealmAuth {
    public static void main(String[] args) {
        //1.读取配置文件吗，生成shiro环境
        Environment environment = new BasicIniEnvironment("classpath:shiroConfig/shiro_realm.ini");
        //2.通过这个环境，生成SecurityManager对象
        SecurityManager securityManager=environment.getSecurityManager();
        //3.shiro工具类需要一个securityManager环境
        SecurityUtils.setSecurityManager(securityManager);

        //4.有环境的工具包，将能获取到一个subject对象，然后我们就能使用subject对象来进行业务开发！
        Subject subject=SecurityUtils.getSubject();
        //5.有了主体用户以后，可以直接执行login来进行登录
        subject.login(new UsernamePasswordToken("leimin","123456"));
        if(subject.isAuthenticated()){
            System.out.println("恭喜用户"+subject.getPrincipals()+"登录成功");
        }
        System.out.println("是否有boss的角色:"+subject.hasRole("boss"));
        System.out.println("是否有吃肉的的动作:"+subject.isPermitted("eat:rou"));
    }
}
